﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
namespace BLL
{
    /// <summary>
    /// Session常用方法封装
    /// </summary>
    public static class Session
    {
        /// <summary>
        /// 检查Session
        /// </summary>
        /// <returns></returns>
        public static bool IsNull(string key)
        {
            try
            {
                if (System.Web.HttpContext.Current.Session[key] == null)
                    return true;
                else
                    return false;
            }
            catch
            {
                return true;
            }
        }
        /// <summary>
        /// 添加Session
        /// </summary>
        public static void Add(string key, object value)
        {
            System.Web.HttpContext.Current.Session.Add(key, value);

        }
        /// <summary>
        /// 修改Session
        /// </summary>
        public static void Update(string key, object value)
        {
            System.Web.HttpContext.Current.Session[key] = value;
        }
        /// <summary>
        /// 移除Session
        /// </summary>
        public static void Remove(string key)
        {
            if (!IsNull(key))
                System.Web.HttpContext.Current.Session[key] = null;
        }
        /// <summary>
        /// 读取Session
        /// </summary>
        public static string ReadString(string key)
        {
            if (IsNull(key))
                return "";
            else
                return System.Web.HttpContext.Current.Session[key].ToString();
        }
        /// <summary>
        /// 读取Session
        /// </summary>
        public static int ReadInt(string key)
        {
            if (IsNull(key))
            {
                return 0;
            }
            else
            {
                int result = 0;
                int.TryParse(ReadString(key), out result);
                return result;
            }
        }
        /// <summary>
        /// 读取Session
        /// </summary>
        public static bool ReadBool(string key)
        {
            if (IsNull(key))
            {
                return false;
            }
            else
            {
                bool result = false;
                bool.TryParse(ReadString(key), out result);
                return result;
            }
        }
        /// <summary>
        /// 读取Session
        /// </summary>
        public static double ReadDouble(string key)
        {
            if (IsNull(key))
            {
                return 0;
            }
            else
            {
                double result = 0;
                double.TryParse(ReadString(key), out result);
                return result;
            }
        }
        /// <summary>
        /// 读取Session
        /// </summary>
        public static List<T> ReadList<T>(string key) where T : new()
        {
            if (IsNull(key))
                return null;
            else
            {
                List<T> tt = System.Web.HttpContext.Current.Session[key] as List<T>;
                return tt;
            }
        }

        /// <summary>
        /// 读取Session
        /// </summary>
        public static T Read<T>(string key) where T : new()
        {
            if (IsNull(key))
                return default(T);
            else
                return (T)System.Web.HttpContext.Current.Session[key];
        }
    }
}
